package com.ruoyi.driver.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.ruoyi.driver.mapper.CarownerDetialsMapper;
import com.ruoyi.driver.domain.CarownerDetials;
import com.ruoyi.driver.service.ICarownerDetialsService;

/**
 * 车主详情Service业务层处理
 * 
 * @author Wevon
 * @date 2024-08-13
 */
@Service
public class CarownerDetialsServiceImpl implements ICarownerDetialsService 
{
    @Autowired
    private CarownerDetialsMapper carownerDetialsMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询车主详情
     * 
     * @param id 车主详情主键
     * @return 车主详情
     */
    @Override
    public CarownerDetials selectCarownerDetialsById(Long id)
    {
        return carownerDetialsMapper.selectCarownerDetialsById(id);
    }

    /**
     * 查询车主详情列表
     * 
     * @param carownerDetials 车主详情
     * @return 车主详情
     */
    @Override
    public List<CarownerDetials> selectCarownerDetialsList(CarownerDetials carownerDetials)
    {
        return carownerDetialsMapper.selectCarownerDetialsList(carownerDetials);
    }

    /*根据手机号查询车主信息*/

    @Override
    public CarownerDetials selectCarOwnerDetailsByPhone(String phone) {
        return carownerDetialsMapper.selectCarOwnerDetailsByPhone(phone);
    }

    /**
     * 新增车主详情,同时新增一条用户信息
     * 
     * @param carownerDetials 车主详情
     * @return 结果
     */
    @Override
    public int insertCarownerDetials(CarownerDetials carownerDetials)
    {
        SysUser sysUser =new SysUser();
        sysUser.setUserName(carownerDetials.getPhone());//用户名:车主手机号
        BCryptPasswordEncoder bCryptPasswordEncoder =new BCryptPasswordEncoder();
        sysUser.setPassword( bCryptPasswordEncoder.encode(carownerDetials.getPassword()));//密码
        sysUser.setNickName(carownerDetials.getCarOwner());//昵称:车主姓名
        sysUser.setRoleIds(new Long[]{100L});//车主角色ID
        sysUser.setPhonenumber(carownerDetials.getPhone());//车主手机号
        sysUser.setRemark("车主用户");//备注
        sysUserService.insertUser(sysUser);
        return carownerDetialsMapper.insertCarownerDetials(carownerDetials);
    }

    /**
     * 修改车主详情
     * 
     * @param carownerDetials 车主详情
     * @return 结果
     */
    @Override
    public int updateCarownerDetials(CarownerDetials carownerDetials)
    {
        return carownerDetialsMapper.updateCarownerDetials(carownerDetials);
    }

    /**
     * 批量删除车主详情
     * 
     * @param ids 需要删除的车主详情主键
     * @return 结果
     */
    @Override
    public int deleteCarownerDetialsByIds(Long[] ids)
    {
        return carownerDetialsMapper.deleteCarownerDetialsByIds(ids);
    }

    /**
     * 删除车主详情信息
     * 
     * @param id 车主详情主键
     * @return 结果
     */
    @Override
    public int deleteCarownerDetialsById(Long id)
    {
        return carownerDetialsMapper.deleteCarownerDetialsById(id);
    }
}
